# -*- coding: UTF-8 -*-

import xeres
import sheets.interface

__all__ = [ 'apply_property' , 'add_property' ]

# all styles properties
__all_properties = {}
__all_category = {}

def assign_property(cat,entity,styles):
	for name,prop in entity.iteritems():
		assert prop.pseudo == cat
		
		entry = __all_properties.get(name)
		if entry == None:
			print "No such property defined (", name, ")!"
			continue
		category,type,call = entry

		# find if category exist
		inst = styles.get(category)
		if inst == None:
			inst = type()
			styles[category] = inst

		call(inst,name,prop.attr)

# Compile all properties in components to legal values
def compile_property(comp):
	c1 = {}
	#print "ready to compile style" , comp.style

	default = comp.style.get(None)
	for cat,pseudo in comp.style.iteritems():

		styles = {}
		c1[cat] = styles
		if pseudo and default:
			assign_property(None,default,styles)
		assign_property(cat,pseudo,styles)
	
	print "compiled style" , c1
	c2 = {}
	for pseudo,styles in c1.iteritems():
		sub = {}
		c2[pseudo] = sub
		for name,prop in styles.iteritems():
			 sub[name] = prop.apply(pseudo,styles)
	
	print "compiled " , c2
	# override compiled styles
	comp.style = c2

# Add new property
def add_property(name,category,type,call):
	if not issubclass(type,sheets.interface.sheet_property):
		print "Style type(%s) must inherit from sheet_property."%(type.__name__)
		return
	if __all_properties.has_key(name):
		print "Style property [",name,"] has been defined."
		return
	if __all_category.has_key(category) and __all_category[category] != type:
		print "Style category [",category,"] has been defined."
		return
	# register properties
	__all_properties[name] = category,type,call
	__all_category[name] = type

# initialize default properties
def __init_default_property():
	from ui.uxml.sheets.interface import IntValueApply
	from ui.uxml.sheets.interface import FloatValueApply
	from ui.uxml.sheets.interface import StrValueApply
	from ui.uxml.sheets.interface import PairIntValueApply
	from ui.uxml.sheets.text_sheets import FontApply
	from ui.uxml.sheets.text_sheets import DecorationApply
	from ui.uxml.sheets.text_sheets import TransformApply
	from ui.uxml.sheets.color_sheets import ColorApply
	from ui.uxml.sheets.placement_sheets import AlignApply
	from ui.uxml.sheets.placement_sheets import PaddingMarginApply
	
	# font styles
	add_property('font','font',FontApply,FontApply.font_all)
	add_property('font-family','font',FontApply,FontApply.font_family)
	add_property('font-size','font',FontApply,FontApply.font_size)
	add_property('font-height','font',FontApply,FontApply.font_size)
	add_property('font-weight','font',FontApply,FontApply.font_weight)
	add_property('font-style','font',FontApply,FontApply.font_italic)
	add_property('font-antialias','font',FontApply,FontApply.font_antialias)

	# basic color styles
	add_property('text-color','text_color',ColorApply,ColorApply.color)
	add_property('text-transparency','text_color',ColorApply,ColorApply.transparency)
	add_property('text-background-color','text_background_color',ColorApply,ColorApply.color)
	add_property('text-background-transparency','text_background_color',ColorApply,ColorApply.transparency)
	add_property('text-edge-color','text_edge_color',ColorApply,ColorApply.color)
	add_property('text-edge-transparency','text_edge_color',ColorApply,ColorApply.transparency)
	add_property('text-shadow-color','text_shadow_color',ColorApply,ColorApply.color)
	add_property('text-shadow-transparency','text_shadow_color',ColorApply,ColorApply.transparency)
	
	# text styles
	add_property('text-decoration','text_decoration',DecorationApply,DecorationApply.set)
	add_property('text-transform','text_transform',TransformApply,TransformApply.set)
	add_property('text-indent','text_decoration',IntValueApply,IntValueApply.set)
	add_property('letter-spacing','letter_spacing',IntValueApply,IntValueApply.set)
	add_property('word-spacing','word_spacing',IntValueApply,IntValueApply.set)
	add_property('text-offset','text_offset',PairIntValueApply,PairIntValueApply.set)

	# placement styles	
	add_property('horizontal-align','horizontal_align',AlignApply,AlignApply.horizontal)
	add_property('vertical-align','vertical_align',AlignApply,AlignApply.vertical)
	
	add_property('padding','padding',PaddingMarginApply,PaddingMarginApply.set)
	add_property('padding-left','padding_left',PaddingMarginApply,PaddingMarginApply.set)
	add_property('padding-right','padding_right',PaddingMarginApply,PaddingMarginApply.set)
	add_property('padding-top','padding_top',PaddingMarginApply,PaddingMarginApply.set)
	add_property('padding-bottom','padding_bottom',PaddingMarginApply,PaddingMarginApply.set)
	
	add_property('margin','margin',PaddingMarginApply,PaddingMarginApply.set)
	add_property('margin-left','margin_left',PaddingMarginApply,PaddingMarginApply.set)
	add_property('margin-right','margin_right',PaddingMarginApply,PaddingMarginApply.set)
	add_property('margin-top','margin_top',PaddingMarginApply,PaddingMarginApply.set)
	add_property('margin-bottom','margin_bottom',PaddingMarginApply,PaddingMarginApply.set)
	
	# gui component styles
	add_property('color','gui_background_color',ColorApply,ColorApply.color)
	add_property('transparency','gui_background_color',ColorApply,ColorApply.transparency)
	add_property('border-color','gui_border_color',ColorApply,ColorApply.color)
	add_property('border-transparency','gui_border_color',ColorApply,ColorApply.transparency)

# initialize default properties
__init_default_property()
